package com.ftg.learn.service.impl;

import com.ftg.learn.entity.User;
import com.ftg.learn.mapper.UserMapper;
import com.ftg.learn.service.Iuser;
import com.ftg.learn.util.BaseResultSet;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.Reader;
import java.util.Objects;

/**
 * 实现类
 *
 * @author kangjx
 * @version 1.0
 * @date 2023/3/24 上午10:50
 */
public class UserImpl implements Iuser {


    Reader reader;
    SqlSession session;

    {
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        try {
            reader = Resources.getResourceAsReader("mybatisconfig.xml");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(reader);

        session = sqlSessionFactory.openSession();

    }
    UserMapper userMapper = session.getMapper(UserMapper.class);



    @Override
    public BaseResultSet loginByUsernamePassword(String uname, String upass) {

        BaseResultSet brs = new BaseResultSet();
        if(null == uname || uname.equals("") ){
            brs.setCode(404);
            brs.setMess("用户名不能为空");
            return brs;
        }

        if(null == upass || upass.equals("")){
            brs.setCode(404);
            brs.setMess("密码不能为空");
            return brs;
        }

        User user = userMapper.checkUserPass(uname,upass);


        if(Objects.isNull(user)){
            brs.setCode(500);
            brs.setMess("对不起用户名或者密码不存在!!!");
        }else{
            brs.setCode(200);
            brs.setMess("登陆成功!!!");
            brs.setData(user);
        }
        return brs;
    }

}
